package com.symone.webcms.schedule;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.symone.webcms.model.ScheduleTask;
import com.symone.webcms.service.ScheduleTaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Order(value = 1)
public class ScheduleInitRunner implements ApplicationRunner {

    private final Logger logger = LoggerFactory.getLogger(ScheduleInitRunner.class);

    @Autowired
    ScheduleTaskService taskService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        logger.info("Starting Schedule Task List ...");
        List<ScheduleTask> taskList = taskService.selectList(new EntityWrapper<ScheduleTask>().eq("status",ScheduleTask.STATUS_RUNNING));
        logger.info("初始化加载定时任务......");
        for (ScheduleTask task : taskList) {
            try {
                taskService.addJob(task);
            } catch (Exception e) {
                logger.error("add job error: " + task.getName() + " " + task.getGroup(), e);
            }
        }
    }
}
